//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.Extensibility
{
    public class AllowDoubleToEnumConversionAttribute : ParameterConverterAttribute
    {
        internal override void ConvertImp(ConversionData data)
        {
            if (typeof (Enum).IsAssignableFrom(data.TargetType))
            {
                if (data.Value is double)
                {
                    double dbl = (double)data.Value;
                    dbl = Math.Floor(dbl);
#if !SILVERLIGHT
                    foreach (Enum e in Enum.GetValues(data.TargetType))
                    {
                        int i = System.Convert.ToInt32(e);
                        if (i == dbl)
                            data.Result = e;
                    }
#endif
                }
            }
        }
    }
}